package cn.edu.fjzzit.web.myhotel.service.impl;

import cn.edu.fjzzit.web.myhotel.mapper.RoleInfoMapper;
import cn.edu.fjzzit.web.myhotel.mapper.UserInfoMapper;
import cn.edu.fjzzit.web.myhotel.mapper.UserRoleMapper;
import cn.edu.fjzzit.web.myhotel.model.UserInfo;
import cn.edu.fjzzit.web.myhotel.model.UserRole;
import cn.edu.fjzzit.web.myhotel.service.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.java2d.pipe.SpanShapeRenderer;

import javax.annotation.Resource;
import java.util.*;

@Service
public class UserServiceImpl implements UserService {
    //@Resource
    @Autowired
    private RoleInfoMapper roleInfoMapper;
    //@Resource
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public UserInfo getUserInfo(String userName) {
        UserInfo userInfo = userInfoMapper.findFirstByUserName(userName);
        return userInfo;
    }

    @Override
    public Set<String> getUserRoles(Integer userId) {
        List<String> roleNameList = roleInfoMapper.queryRoleNameByUserId(userId);
        Set<String> roles = new HashSet<>(roleNameList);
        return roles;
    }

    @Override
    public Set<String> getUserPermissions(Integer userId) {
        return null;
    }

    @Override
    public void addUser(String userName, String password, String roleName) throws Exception {
        Integer roleId = roleInfoMapper.findRoleIdByRoleName(roleName);
        if (roleId == null) {

            throw new Exception();
        }
        String salt = UUID.randomUUID().toString().replace("-", "");
        SimpleHash simpleHash = new SimpleHash(Md5Hash.ALGORITHM_NAME, password, salt, 3);

        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setSalt(salt);
        userInfo.setPassword(simpleHash.toString());
        userInfo.setCreateTime(new Date());
        userInfo.setLastTime(null);
        userInfo.setUserState(Byte.parseByte("0"));
        userInfoMapper.insert(userInfo);

        Integer userId = userInfoMapper.findFirstByUserName(userName).getUserId();
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        userRole.setRoleId(roleId);
        userRoleMapper.insert(userRole);
    }

    public void removeUser(String userName) throws Exception {
        Integer userId = userInfoMapper.findFirstByUserName(userName).getUserId();
        if (userId == null) {
            throw new Exception();
        }
        userRoleMapper.remove(userId);
        userInfoMapper.remove(userName);
    }

    public void modifyPassword(String userName, String password) {
        UserInfo userInfo = userInfoMapper.findFirstByUserName((userName));
        SimpleHash simpleHash = new SimpleHash(Md5Hash.ALGORITHM_NAME, password, userInfo.getSalt(), 3);

        userInfoMapper.updatePasswordByUserName(simpleHash.toString(), userName);

    }


    public boolean verifyPassword(String password, String salt, String cryptograph) {
        SimpleHash simpleHash = new SimpleHash(Md5Hash.ALGORITHM_NAME, password, salt, 3);
        if (cryptograph.equals(simpleHash.toString())) {
            return true;
        } else {
            return false;
        }


    }

    @Override
    public void resetPassword(String userName) throws Exception {
        //首先我们通过用户名取到用户ID
        int userID = userInfoMapper.findFirstByUserName(userName).getUserId();
        //初始化密码为123456
        String password = "123456";
        //利用UUID生成随机的盐值
        String salt = UUID.randomUUID().toString().replace("-","");
        //使用MD5算法和盐值生成密文
        SimpleHash simpleHash = new SimpleHash(Md5Hash.ALGORITHM_NAME,password,salt,512);
        //修改数据库中对应用户的盐值和密文
        userInfoMapper.updateUserPasswordByUserID(userID,simpleHash.toString(),salt);
    }


    @Override
    public void resetSelfPassword(String userName, String password) throws Exception {
        //这里业务逻辑和管理员重置密码几乎一样
        //唯一的差别就是用户重置的密码的通过前端传来的, 而不是一个固定的值
        //注意考虑考虑代码复用的问题, 这就留给你们自己思考啦!
        //通过用户名取得用户ID
        int userID = userInfoMapper.findFirstByUserName(userName).getUserId();
        //使用UUID生成随机的盐值
        String salt = UUID.randomUUID().toString().replace("-","");
        //通过MD5算法和盐值共同生成密文
        SimpleHash simpleHash = new SimpleHash(Md5Hash.ALGORITHM_NAME,password,salt,3);
        //修改数据库中对应用户的盐值和密文
        userInfoMapper.updateUserPasswordByUserID(userID,simpleHash.toString(),salt);
    }

}





